
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yp/helper.dart';

import '../../../router.dart';
import 'logic.dart';
import 'state.dart';

class CollectCouponsCentrePage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _CollectCouponsCentrePage();
  }
}

class _CollectCouponsCentrePage extends State<CollectCouponsCentrePage> {
  final CollectCouponsCentreLogic logic = Get.put(CollectCouponsCentreLogic());
  final CollectCouponsCentreState state = Get.find<CollectCouponsCentreLogic>().state;


  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        Get.back(result: true);
        return true;
      },
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          elevation: 0,
          backgroundColor: Colors.white,
          title: Text(
            '领券中心',
            style: TextStyle(
              color: Color(0xff333333),
              fontSize: 18,
            ),
          ),
          iconTheme: IconThemeData(
            color: Color(0xff666666), //修改颜色
          ),
        ),
        body: GetBuilder<CollectCouponsCentreLogic>(
          builder: (logic) {
            return Container(
              color: Color(0xffefefef),
              child: Column(
                children: [

                  Expanded(
                    child: Container(
                      child: state.couponsCentreList.length >= 0 ? SmartRefresher(
                        controller: state.controller1,
                        header: CustomHeader(
                          builder: (BuildContext context, RefreshStatus mode) {
                            Widget body;
                            if (mode == RefreshStatus.completed) {
                              body = CupertinoActivityIndicator();
                            }
                            return Container(
                              height: 55.0,
                              child: Center(child: body),
                            );
                          },
                        ),
                        footer: CustomFooter(
                          builder: (BuildContext context, LoadStatus mode) {
                            Widget body;
                            if (mode == LoadStatus.idle) {
                              body = Text("没有更多数据了");
                            } else if (mode == LoadStatus.loading) {
                              body = CupertinoActivityIndicator();
                            } else if (mode == LoadStatus.failed) {
                              body = Text("刷新失败");
                            } else {
                              body = Text("没有更多数据了");
                            }
                            return Container(
                              height: 55.0,
                              child: Center(child: body),
                            );
                          },
                        ),
                        onRefresh: logic.refreshData,
                        // onLoading: logic.loadMoreData,
                        enablePullUp: false,
                        enablePullDown: true,
                        child: ListView.separated(
                          scrollDirection: Axis.vertical,
                          padding: EdgeInsets.symmetric(vertical: setWidth(30)),
                          itemBuilder: (context, index) {
                            return _couponsItem(index: index);
                          },
                          separatorBuilder: (BuildContext context, int index) {
                            return SizedBox(
                              height: setWidth(20),
                            );
                          },
                          itemCount: state.couponsCentreList.length,
                        ),
                      ) : Center(
                        child: Column(
                          children: [
                            SizedBox(
                              height: 140.w,
                            ),
                            Image(
                              image: AssetImage("assets/images/nodata.png"),
                              width: 218.w,
                              height: 164.w,
                            ),
                            SizedBox(
                              height: 20.w,
                            ),
                            Text(
                              '暂无数据',
                              style: TextStyle(
                                color: Color(0xff999999),
                                fontSize: 28.w,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  Container(
                    width: MediaQuery.of(context).size.width,
                    height: setWidth(101),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      border: Border(
                        top: BorderSide(
                          color: Color(0xffefefef),
                          width: setWidth(1),
                        )
                      )
                    ),
                    child: Row(
                      children: [
                        InkWell(
                          onTap: () {
                            print('+++++ 领券中心 +++++');
                            // logic.onChooseChange(0);
                          },
                          child: Container(
                            height: setWidth(100),
                            width: setWidth(374),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Container(
                                  height: setWidth(92),
                                  alignment: Alignment.center,
                                  child: Text(
                                    '领券中心',
                                    style: TextStyle(
                                      color: state.chooseIndex == 0 ? Color(0xffC99F4F) : Color(0xff999999),
                                      fontSize: state.chooseIndex == 0 ? 18 : 15,
                                    ),
                                  ),
                                ),
                                Container(
                                  width: setWidth(126),
                                  height: setWidth(8),
                                  color: state.chooseIndex == 0 ? Color(0xffC99F4F) : Colors.transparent,
                                ),
                              ],
                            ),
                          ),
                        ),
                        SizedBox(
                          width: setWidth(2),
                        ),
                        InkWell(
                          onTap: () {
                            print('+++++ 我的优惠券 +++++');
                                 print('22');
                            // logic.onChooseChange(1);
                             Get.offNamed(RouteConfig.myCoupon);

                          },
                          child: Container(
                            height: setWidth(100),
                            width: setWidth(374),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Container(
                                  height: setWidth(92),
                                  alignment: Alignment.center,
                                  child: Text(
                                    '我的优惠券',
                                    style: TextStyle(
                                      color: state.chooseIndex == 0 ? Color(0xff999999) : Color(0xffC99F4F),
                                      fontSize: state.chooseIndex == 0 ? 15 : 18,
                                    ),
                                  ),
                                ),
                                Container(
                                  width: setWidth(126),
                                  height: setWidth(8),
                                  color: state.chooseIndex == 0 ? Colors.transparent : Color(0xffC99F4F),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }

  // item
  Widget _couponsItem({
    int index,
  }) {
    Map<String, dynamic> dataMap = state.couponsCentreList[index];
    // print(' 进度百分比  ${} ');

    return Padding(
      padding: EdgeInsets.only(
        left: setWidth(26),
        right: setWidth(22),
      ),
      child: Container(
        height: setWidth(210),
        color: Colors.white,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
              width: setWidth(518),
              height: setWidth(210),
              padding: EdgeInsets.symmetric(horizontal: setWidth(22)),
              child: Row(
                children: [
                  Image.asset(
                    dataMap['type_id'] == 2 ? 'assets/images/discountCoupon_image.png' : 'assets/images/priceCoupon_image.png',
                    width: setWidth(106),
                    height: setWidth(106),
                    fit: BoxFit.cover,
                  ),
                  SizedBox(
                    width: setWidth(26),
                  ),
                  Expanded(
                    child: Container(
                      height: setWidth(210),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            '【${dataMap['type_id'] == 1 ? '现金券' : dataMap['type_id'] == 2 ? '折扣券' : '免邮券'}】${dataMap['title']}',
                            style: TextStyle(
                              color: Color(0xff222222),
                              fontSize: 13,
                            ),
                          ),
                          SizedBox(
                            height: setWidth(32),
                          ),
                          dataMap['type_id'] == 1 ? Text.rich(
                            TextSpan(
                              children: [
                                TextSpan(
                                  text: '￥${dataMap['face_value']}',
                                  style: TextStyle(
                                    color: Color(0xffC99F4F),
                                    fontSize: 20,
                                  ),
                                ),
                                TextSpan(
                                  text: dataMap['apply_type'] == 0 ? '全部商品可用' : '部分商品可用',
                                  style: TextStyle(
                                    color: Color(0xffC99F4F),
                                    fontSize: 12,
                                  )
                                ),
                              ],
                            ),
                          ) : dataMap['type_id'] == 2 ? Text.rich(
                            TextSpan(
                              children: [
                                TextSpan(
                                  // text: parsePrice(double.parse(dataMap['face_value'].toString()) / 10),
                                  text:logic.faceValue(dataMap['face_value'].toString()),
                                  style: TextStyle(
                                    color: Color(0xffC99F4F),
                                    fontSize: 22,
                                  ),
                                ),
                                TextSpan(
                                  text: '折',
                                  style: TextStyle(
                                    color: Color(0xffC99F4F),
                                    fontSize: 15,
                                  ),
                                ),
                                WidgetSpan(
                                  child: SizedBox(
                                    width: setWidth(10),
                                  ),
                                ),
                                TextSpan(
                                  text: '满${parsePrice(dataMap['least_cost'])}元可用',
                                  style: TextStyle(
                                    color: Color(0xffC99F4F),
                                    fontSize: 12,
                                  ),
                                ),
                              ],
                            ),
                          ): Text.rich(
                            TextSpan(
                              children: [
                                TextSpan(
                                  text: '满${dataMap['face_value']}元可用',
                                  style: TextStyle(
                                    color: Color(0xffC99F4F),
                                    fontSize: 12,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Container(
              width: setWidth(184),
              height: setWidth(210),
              padding: EdgeInsets.symmetric(horizontal: setWidth(18)),
              child: dataMap['stock_qty'] <= 0 ? Container( // 已抢光
                width: setWidth(184),
                height: setWidth(210),
                alignment: Alignment.center,
                child: Image.asset(
                  'assets/images/robbedAll_image.png',
                  width: setWidth(118),
                  height: setWidth(118),
                  fit: BoxFit.cover,
                ),
              ) : dataMap['is_receive_count'] < dataMap['limit_usr_number'] ? Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '剩余${dataMap['percent']}',
                      style: TextStyle(
                        color: Color(0xff333333),
                        fontSize: 10,
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(
                        top: setWidth(10),
                        bottom: setWidth(22),
                      ),
                      child: Stack(
                        children: [
                          Container(
                            width: setWidth(140),
                            height: setWidth(10),
                            decoration: BoxDecoration(
                              color: Color(0xffefefef),
                              borderRadius: BorderRadius.circular(setWidth(6)),
                            ),
                          ),
                          Container(
                            width: setWidth(140) * double.parse(dataMap['percent'].toString().replaceAll('%', '')) / 100,
                            height: setWidth(10),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(setWidth(6)),
                              gradient: LinearGradient(
                                begin: Alignment.centerLeft,
                                end: Alignment.centerRight,
                                colors: [
                                  Color(0xffC99F4F),
                                  Color(0xffF4D290),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Text(
                      '限领${dataMap['limit_usr_number']}张 已领${dataMap['is_receive_count']}张',
                      style: TextStyle(
                        color: Color(0xff999999),
                        fontSize: 10,
                      ),
                    ),
                    SizedBox(
                      height: setWidth(20),
                    ),
                    InkWell(
                      onTap: () {
                        print('================== 立即领取点击事件 ================== index = $index');
                        logic.getCoupon(
                          coupon_num_id: dataMap['series'].toString(),
                          index: index,
                          context: context,
                        );
                      },
                      child: Container(
                        width: setWidth(140),
                        height: setWidth(44),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(setWidth(22)),
                          gradient: LinearGradient(
                            begin: Alignment.centerLeft,
                            end: Alignment.centerRight,
                            colors: [
                              Color(0xffF4D290),
                              Color(0xffC99F4F),
                            ],
                          ),
                        ),
                        alignment: Alignment.center,
                        child: Text(
                          '立即领取',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 12,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ) : Container( // 已领需要处理
                width: setWidth(184),
                height: setWidth(210),
                alignment: Alignment.center,
                child: Image.asset(
                  'assets/images/received_image.png',
                  width: setWidth(118),
                  height: setWidth(118),
                  fit: BoxFit.cover,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  _shareShow() {
    return showDialog(
      context: context,
      useSafeArea: false,
      builder: (context) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Container(
              width: MediaQuery.of(context).size.width,
              color: Colors.white,
              padding: EdgeInsets.only(
                left: setWidth(40),
                top: setWidth(46),
                right: setWidth(40),
                bottom: setWidth(34),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: setWidth(16)),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Stack(
                          children: [
                            Image.asset(
                              'assets/images/coupons_images.png',
                              width: setWidth(638),
                              height: setWidth(180),
                              fit: BoxFit.cover,
                            ),

                            Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Container(
                                  height: setWidth(180),
                                  width: setWidth(166),
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Text.rich(
                                        TextSpan(
                                          children: [
                                            TextSpan(
                                              text: '￥',
                                              style: TextStyle(
                                                color: Color(0xffFFFFFE),
                                                fontSize: 12,
                                              ),
                                            ),
                                            TextSpan(
                                              text: '10',
                                              style: TextStyle(
                                                color: Color(0xffFFFFFE),
                                                fontSize: 25,
                                              ),
                                            ),
                                          ],
                                        )
                                      ),
                                      Text(
                                        '现金券',
                                        style: TextStyle(
                                          color: Colors.white,
                                          fontSize: 12,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  height: setWidth(180),
                                  width: setWidth(472),
                                  padding: EdgeInsets.only(
                                    left: setWidth(28),
                                    top: setWidth(28),
                                    right: setWidth(28),
                                  ),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                        '嘉定万达开业活动、门店使用嘉定万达开业活动、门店使用',
                                        maxLines: 2,
                                        overflow: TextOverflow.ellipsis,
                                        style: TextStyle(
                                          color: Colors.white,
                                          fontSize: 14,
                                          height: 1.3,
                                        ),
                                      ),
                                      Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            '2020.02.19～2020.02.20',
                                            style: TextStyle(
                                              color: Colors.white,
                                              fontSize: 10,
                                            ),
                                          ),
                                          SizedBox(
                                            height: setWidth(6),
                                          ),
                                          Container(
                                            height: setWidth(52),
                                            alignment: Alignment.centerLeft,
                                            padding: EdgeInsets.only(
                                              bottom: setWidth(2),
                                            ),
                                            decoration: BoxDecoration(
                                              border: Border(
                                                top: BorderSide(
                                                  color: Colors.white.withOpacity(0.3),
                                                  width: setWidth(1),
                                                ),
                                              ),
                                            ),
                                            child: Text(
                                              '周一至周末可用',
                                              style: TextStyle(
                                                color: Colors.white,
                                                fontSize: 12,
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                        Padding(
                          padding: EdgeInsets.symmetric(vertical: setWidth(16)),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Text(
                                '已售：2131件',
                                style: TextStyle(
                                  color: Color(0xff999999),
                                  fontSize: 12,
                                ),
                              ),
                              SizedBox(
                                width: setWidth(14),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),

                  Text(
                    '券使用规则',
                    style: TextStyle(
                      color: Color(0xff333333),
                      fontSize: 14,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(
                      top: setWidth(30),
                      bottom: setWidth(80),
                    ),
                    child: Text(
                      '限购张数：不限制\n有效期：2020.02.19～2020.02.20\n使用时段：\n使用范围：部分商品（门店指定商品）\n可与其他活动通享\n可与其他券同享\n可与储值卡同享\n允许转增\n退单时返回\n不参与积分\n适用门店：部分门店',
                      style: TextStyle(
                        color: Color(0xff666666),
                        fontSize: 12,
                        height: 1.5,
                      ),
                    ),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text.rich(
                        TextSpan(
                          children: [
                            TextSpan(
                              text: '￥',
                              style: TextStyle(
                                color: Color(0xffC99F4F),
                                fontSize: 12,
                              ),
                            ),
                            TextSpan(
                              text: '10',
                              style: TextStyle(
                                color: Color(0xffC99F4F),
                                fontSize: 25,
                              ),
                            ),
                            WidgetSpan(
                              child: SizedBox(
                                width: setWidth(18),
                              ),
                            ),
                            TextSpan(
                              text: '¥50',
                              style: TextStyle(
                                color: Color(0xff999999),
                                fontSize: 12,
                                decoration: TextDecoration.lineThrough,
                              ),
                            ),
                          ],
                        ),
                      ),

                      InkWell(
                        onTap: () {
                          print('****************** 立即抢购点击事件 ******************');
                        },
                        child: Container(
                          height: setWidth(88),
                          width: setWidth(360),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(setWidth(44)),
                            gradient: LinearGradient(
                              begin: Alignment.centerLeft,
                              end: Alignment.centerRight,
                              colors: [
                                Color(0xffDFC595),
                                Color(0xffC99F4F),
                              ],
                            ),
                          ),
                          alignment: Alignment.center,
                          child: Text(
                            '立即抢购',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 18,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        );
      },
    );
  }


}
